﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VendorWindow : Window
{
   
    [SerializeField]
    private VendorButton[] vendorButtons;
    private List<List<VendorItem>> pages = new List<List<VendorItem>>();
    private int pagenumber;
    [SerializeField]
    private Text pageNumberText;
    [SerializeField]
    private GameObject previousPage, nextPage;
    private Vendor vendor;
    
    public void CreatePages(VendorItem[] items)
    {
        pages.Clear();
        List<VendorItem> page = new List<VendorItem>();
        for (int i = 0; i < items.Length; i++)
        {
            page.Add(items[i]);
            if (page.Count == 10 || i == items.Length - 1)
            {
                pages.Add(page);
                page = new List<VendorItem>();
            }
        }
        AddItems();
    }
    private void AddItems()
    {
        
        if (pages.Count > 0)
        {
            pageNumberText.text = pagenumber + 1 + "/" + pages.Count;
            previousPage.SetActive(pagenumber > 0);
            nextPage.SetActive(pages.Count > 1 && pagenumber < pages.Count - 1);

            for (int i = 0; i < pages[pagenumber].Count; i++)
            {
                if (pages[pagenumber][i] != null)
                {
                    vendorButtons[i].AddItem(pages[pagenumber][i]);
                }
            }
        }
    }
    public void PrevPage()
    {
        if (pagenumber > 0)
        {
            ClearButtons();
            pagenumber--;
            AddItems();
        }
    }
    public void NextPage()
    {
        if (pagenumber <(pages.Count-1))
        {
            ClearButtons();
            pagenumber++;
            AddItems();
        }
    }
    public void ClearButtons()
    {
        foreach(VendorButton button in vendorButtons)
        {
            button.gameObject.SetActive(false);
        }
    }
    public override void Open(npc nPC)
    {
        CreatePages((nPC as Vendor).MyItems);
        base.Open(nPC);
        
    }
    public override void Close()
    {
        ClearButtons();
        base.Close();
    }
}

